package array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class MergeArray {
    public static void main(String[] args) {
        MergeArray mergeArray = new MergeArray();
        int[] ints = {1,2,3,0,0,0};
        int[] ints1 = {2,5,6};
        mergeArray.solution(ints, 3, ints1, 3);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }
    public void solution(int[] nums1, int m, int[] nums2, int n) {
        if (nums1 == null || nums2 == null) {
            return;
        }
        if(n==0)return;
        int mp = m - 1;
        int np = n - 1;
        int tail = m + n - 1;
        for (int i = tail; i >= 0; i--) {
            if(mp < 0){
                nums1[i] = nums2[np];
                np--;
            }else if(np < 0){
                nums1[i] = nums1[mp];
                mp--;
            }else{
                if(nums1[mp] > nums2[np]){
                    nums1[i] = nums1[mp];
                    mp--;
                }else {
                    nums1[i] = nums2[np];
                    np--;
                }
            }

        }
    }
}
